package com.lich.service.graph;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lich.dao.ClientLoginMapper;
import com.lich.dao.graph.DayLoginTimesMapper;
import com.lich.dao.graph.MonthLoginTimesMapper;
import com.lich.dao.graph.UserDayLoginTimesMapper;
import com.lich.dao.graph.WeekLoginTimesMapper;
import com.lich.entity.ClientLogin;
import com.lich.entity.graph.ClientAnalysis;

@Service
public class ClientAnalysisService {
	@Resource
	DayLoginTimesMapper dayLoginTimesMapper;
	@Resource
	WeekLoginTimesMapper weekLoginTimesMapper;
	@Resource
	MonthLoginTimesMapper monthLoginTimesMapper;
	@Resource
	UserDayLoginTimesMapper userDayLoginTimesMapper;
	@Resource
	ClientLoginMapper clientLoginMapper;
	
	/**
	 * @description:查找日登陆的次数,并封装到bean中
	 * @author: yuanyh 
	 * @Time: 2016年9月14日  
	 * @param clientLoginTimes 用于封装数据与参数
	 * @return 封装数据的bean
	 */
	public ClientAnalysis findDayLoginTimes(ClientAnalysis clientAnalysis){
		
		List<ClientAnalysis> clientAnalysisList = new ArrayList<ClientAnalysis>();
		
		ClientAnalysisService.defaultParam(clientAnalysis);
		
		clientAnalysisList = dayLoginTimesMapper.selectDayLoginTiemsByParam(clientAnalysis);
		
		ClientAnalysisService.dataDispose(clientAnalysisList,clientAnalysis);
		
		return clientAnalysis;
	}
	
	/**
	 * @description:查找周登陆的次数,并封装到bean中
	 * @author: yuanyh 
	 * @Time: 2016年9月14日  
	 * @param clientLoginTimes 用于封装数据与参数
	 * @return 封装数据的bean
	 */
	public ClientAnalysis findWeekLoginTimes(ClientAnalysis clientAnalysis){
		
		List<ClientAnalysis> clientAnalysisList = new ArrayList<ClientAnalysis>();
		
		ClientAnalysisService.defaultParam(clientAnalysis);
		
		clientAnalysisList = weekLoginTimesMapper.selectWeekLoginTiemsByParam(clientAnalysis);
		
		ClientAnalysisService.dataDispose(clientAnalysisList,clientAnalysis);
		
		return clientAnalysis;
	}
	/**
	 * @description:查找月登陆的次数,并封装到bean中
	 * @author: yuanyh 
	 * @Time: 2016年9月14日  
	 * @param clientLoginTimes 用于封装数据与参数
	 * @return 封装数据的bean
	 */
	public ClientAnalysis findMonthLoginTimes(ClientAnalysis clientAnalysis){
		
		List<ClientAnalysis> clientAnalysisList = new ArrayList<ClientAnalysis>();
		
		ClientAnalysisService.defaultParam(clientAnalysis);
		
		clientAnalysisList = monthLoginTimesMapper.selectMonthLoginTiemsByParam(clientAnalysis);
		
		ClientAnalysisService.dataDispose(clientAnalysisList,clientAnalysis);
		
		return clientAnalysis;
	}
	/**
	 * @description:按自定义日期查找客户端登陆的信息,并封装到bean中
	 * @author: yuanyh 
	 * @Time: 2016年9月14日  
	 * @param clientLoginTimes 用于封装数据与参数
	 * @return 封装数据的bean
	 */
	public List<ClientLogin> findClientLoginByDate(ClientAnalysis clientAnalysis){
		return clientLoginMapper.selectByDate(clientAnalysis);
	}
	
	/**
	 * @description:图表数据处理
	 * @author: yuanyh 
	 * @Time: 2016年9月13日  
	 * @param clientLoginTimesList 数据队列
	 * @param clientLoginTimes 数据的封装
	 */
	private static void dataDispose(List<ClientAnalysis> clientAnalysisList,ClientAnalysis clientAnalysis){
		List<Integer> dataList = new ArrayList<Integer>();
		List<Date> xAxleList = new ArrayList<Date>();
		for(ClientAnalysis dlt : clientAnalysisList){
			if(dlt.getLoginTimes()==null){
				dataList.add(dlt.getClientNum());
			}else{
				dataList.add(dlt.getLoginTimes());
			}
			xAxleList.add(dlt.getStatisticsDate());
		}
		clientAnalysis.setDataList(dataList);
		clientAnalysis.setxAxleList(xAxleList);
	}
	
	
	/**
	 * @description: 设置默认日期参数
	 * @author: yuanyh 
	 * @Time: 2016年9月13日  
	 * @param clientLoginTimes 参数的封装
	 */
	public static void defaultParam(ClientAnalysis clientAnalysis){
		if(clientAnalysis.getDayNum()==null){
			clientAnalysis.setDayNum(-7);
		}
		if(clientAnalysis.getEndDate() == null || clientAnalysis.getStartDate() == null){
			clientAnalysis.setStartDate(ClientAnalysisService.beforNumDay(clientAnalysis.getDayNum()+1));
			clientAnalysis.setEndDate(ClientAnalysisService.beforNumDay(0));
		}
		String endtDate = clientAnalysis.getEndDate()+" 23:59:59";
		clientAnalysis.setEndDate(endtDate);
	}
	/**
	* @description: 查询当前日期前x天的日期
    * @author yuanyh
    * @param day 天数（如果day数为负数,说明是此日期前的天数）
    * @return Date 日期
    */
    public static String beforNumDay(int day){
    	Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newDate = sdf.format(c.getTime());
     //   System.out.println(newDate);
        return newDate;
    }
}
